﻿using Apps.Models.PLAN;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wantn.FIS.Enum;

namespace Apps.DAL.PLAN
{
    public partial class PLAN_PURCHASE_DETAILRepository
    {
        public IQueryable<PlanDetailModel> QueryPlanDetailList(string strSupplierID, int iBusinessType, int iYear, int iStartMonth, int iEndMonth)
        {
            if (string.IsNullOrWhiteSpace(strSupplierID)) strSupplierID = "$--Empty SupplierID--$";
            if (iYear == 0) iYear = DateTime.Now.Year;
            if (iStartMonth == 0) iStartMonth = -1;
            if (iEndMonth == 0) iEndMonth = -1;

            IQueryable<PlanDetailModel> result = from ppd in Context.PLAN_PURCHASE_DETAIL
                                                 join pp in Context.PLAN_PURCHASE on ppd.PURCHASE_ID equals pp.PURCHASE_ID into spp
                                                 from tspp in spp.DefaultIfEmpty()
                                                 join ps in Context.PUB_SUPPLIER on ppd.SUPPLIER_ID equals ps.SUPPLIER_ID into sps
                                                 from tsps in sps.DefaultIfEmpty()
                                                 join pm in Context.PUB_MINE on ppd.MINE_ID equals pm.MINE_ID into spm
                                                 from tspm in spm.DefaultIfEmpty()
                                                 join pmt in Context.PUB_MATERIEL on ppd.MATERIEL_ID equals pmt.MATERIEL_ID into spmt
                                                 from tspmt in spmt.DefaultIfEmpty()
                                                 where (ppd.SUPPLIER_ID == strSupplierID || strSupplierID == "$--Empty SupplierID--$")
                                                 && tspp.YEAR == iYear
                                                 && (tspp.MONTH >= iStartMonth || iStartMonth == -1)
                                                 && (tspp.MONTH <= iEndMonth || iEndMonth == -1)
                                                 select new PlanDetailModel()
                                                 {
                                                     ARRIVE_STANDARD_PRICE = ppd.ARRIVE_STANDARD_PRICE,
                                                     FACTORY_PRICE = ppd.FACTORY_PRICE,
                                                     MATERIEL_ID = ppd.MATERIEL_ID,
                                                     MATERIEL_NAME = tspmt.MATERIEL_NAME,
                                                     MINE_ID = ppd.MINE_ID,
                                                     MINE_NAME = tspm.MINE_NAME,
                                                     MONTH = tspp.MONTH,
                                                     PlanDetailID = tspp.ODD_NUMBERS,
                                                     PlanID = tspp.PURCHASE_ID,
                                                     PLAN_COUNT = ppd.PLAN_COUNT,
                                                     PLAN_TERM = ppd.PLAN_TERM,
                                                     YEAR = tspp.YEAR,
                                                     PLAN_TYPE = (int)BusinessType.MPurchasePlan,
                                                     PRICE_TYPE = ppd.PRICE_TYPE,
                                                     SUPPLIER_ID = ppd.SUPPLIER_ID,
                                                     SUPPLIER_NAME = tsps.SUPPLIER_NAME,
                                                     CONTRACTPRICE = ppd.CONTRACTPRICE,
                                                     ECONTRACT_CODE = ppd.ECONTRACT_CODE,
                                                     TYPE_NAME = "燃料月度采购计划"
                                                 };
            return result;
        }
    }
}
